package com.junzhiit.nengji.shop.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.dao.WechatConfigDAO;
import com.junzhiit.nengji.dao.cache.WechatConfigCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.WechatConfig;
import com.junzhiit.nengji.shop.service.SendPhoneService;
import com.junzhiit.nengji.shop.service.UserService;
import com.junzhiit.nengji.shop.web.exception.LoginResponseNotFoundException;
import com.junzhiit.nengji.shop.web.exception.WechatConfigAddException;
import com.junzhiit.nengji.shop.web.util.aliyun.SmsUtil;
import com.junzhiit.nengji.shop.web.util.wechat.WeChatUtils;
import com.junzhiit.nengji.shop.web.util.wechat.bean.LoginResponse;
import com.junzhiit.nengji.shop.web.vo.LoginVO;
import com.junzhiit.nengji.shop.web.vo.ResultVO;
import com.junzhiit.nengji.shop.web.vo.UserEditVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController{

	@Autowired
	private UserService userService;
	
	@Autowired
	private SendPhoneService sendPhoneService;
	
	@Autowired
	private WechatConfigCacheDAO wechatConfigCacheDAO;
	
	@Autowired
	private WechatConfigDAO wechatConfigDAO;
	
	@ApiOperation(value = "查询用户信息ById")
	@RequestMapping(value = "/{id}",method = RequestMethod.GET)
	public ResultVO getUserById(@PathVariable String id){
		AssertUtil.notBlank(id, "id is required.");
		return userService.getUserById(id);
	}
	
	@ApiOperation(value="授权")
	@RequestMapping(value = "/authorization",method = RequestMethod.POST)
	public ResultVO authorization(LoginVO loginVO){
		AssertUtil.notBlank(loginVO.getLoginCode(), "LoginCode is required.");
		AssertUtil.notBlank(loginVO.getWcId(), "WcId is required.");
		String key = DIYKey.getWechatCodeKey(loginVO.getWcId());
		WechatConfig wechatConfig = wechatConfigCacheDAO.getHash(key, WechatConfig.class);
		if(wechatConfig == null){
			wechatConfig = wechatConfigDAO.getWechatConfigByWcId(loginVO.getWcId());
			boolean is  = wechatConfigCacheDAO.addHash(key, wechatConfig);
			if(!is){
				throw new WechatConfigAddException("找不到微信配置");
			}
		}
		LoginResponse response = WeChatUtils.authorization(loginVO.getLoginCode(),wechatConfig);
		if(StringUtils.isEmpty(response.getOpenId())){
		 throw new LoginResponseNotFoundException("授权失败");
		}
		loginVO.setCsId(wechatConfig.getCsId());
		return userService.userAuth(loginVO, response);
	}
	
	@ApiOperation(value = "修改用户个人信息")
	@RequestMapping(method = RequestMethod.PUT)
	public ResultVO edit(UserEditVO userEditVO) {
		AssertUtil.notBlank(userEditVO.getId(), "id is required.");
		return userService.edit(userEditVO);
	}
	
	@ApiOperation(value = "发送邮箱验证码")
	@RequestMapping(value = "/send/email",method = RequestMethod.POST)
	public ResultVO sendEmailCode(String userId,String email) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		return userService.sendEmailCode(userId,email);
	}
	
	@ApiOperation(value = "绑定邮箱/修改绑定邮箱")
	@RequestMapping(value = "/bind/email",method = RequestMethod.PUT)
	public ResultVO bindEmail(String userId,String email,String code) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.bindEmail(userId,email,code);
	}
	
	@ApiOperation(value = "发送手机验证码")
	@RequestMapping(value = "/send/phone",method = RequestMethod.POST)
	public ResultVO sendMobilePhoneCode(String userId,String mobilePhone) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		return sendPhoneService.sendPhoneCodeByUserId(userId, mobilePhone, SmsUtil.ALIYUN_SMS_CHECKED_CODE);
	}
	
	@ApiOperation(value = "发送手机验证码不需要userId")
	@RequestMapping(value = "/send/phone/code",method = RequestMethod.POST)
	public ResultVO sendMobilePhoneCode(String mobilePhone) {
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		return sendPhoneService.sendPhoneCode(mobilePhone, SmsUtil.ALIYUN_SMS_CHECKED_CODE);
	}
	
	@ApiOperation(value = "绑定手机号码/修改绑定手机号码")
	@RequestMapping(value = "/bind/phone",method = RequestMethod.PUT)
	public ResultVO bindMobilePhone(String userId,String mobilePhone,String code,String name) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(mobilePhone, "mobilePhone is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.bindMobilePhone(userId,mobilePhone,code,name);
	}
	
	@ApiOperation(value = "邮箱验证-发送验证码")
	@RequestMapping(value = "/email/check/send",method = RequestMethod.POST)
	public ResultVO emailCheckSendCode(String userId,String email) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		return userService.emailCheckSendCode(userId,email);
	}
	
	@ApiOperation(value = "邮箱验证")
	@RequestMapping(value = "/email/check",method = RequestMethod.PUT)
	public ResultVO emailCheck(String userId,String email,String code) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(email, "email is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.emailCheck(userId,email,code);
	}
	
	@ApiOperation(value = "手机号码验证-发送短信验证码")
	@RequestMapping(value = "/phone/check/send",method = RequestMethod.POST)
	public ResultVO mobilePhoneCheckSendCode(String userId) {
		AssertUtil.notBlank(userId, "userId is required.");
		return sendPhoneService.sendPhoneCodeByUserId(userId, SmsUtil.ALIYUN_SMS_CHECKED_CODE);
	}
	
	@ApiOperation(value = "手机号码验证")
	@RequestMapping(value = "/phone/check",method = RequestMethod.PUT)
	public ResultVO mobilePhoneCheck(String userId,String code) {
		AssertUtil.notBlank(userId, "userId is required.");
		AssertUtil.notBlank(code, "code is required.");
		return userService.mobilePhoneCheck(userId,code);
	}
	
	@ApiOperation(value = "用户积分")
	@RequestMapping(value = "/config",method = RequestMethod.GET)
	public ResultVO config(String userId){
		AssertUtil.notBlank(userId, "userId is required.");
		return userService.getUserConfig(userId);
	}
}
